using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour
{
    public float speed = 5f;

    private int maxHealth = 5;
    private int currentHealth;

    public int MyMaxHealth { get { return maxHealth; } }
    public int MyCurrentHealth { get { return currentHealth; } }
    private float invincibleTime = 2f;
    private float invincibleTimer ;
    private bool isInvincible;

    public GameObject bulletPrefab;

    public AudioClip hitClip;
    public AudioClip launchClip;


    

    private Vector2 lookDirection = new Vector2(1,0);

    [SerializeField]
    private int maxBulletCount=99;
    private int currentBulletCount;
    

    public int MyCurrentBulletCount { get { return currentBulletCount; } }
    public int MyMaxBulletCount { get { return maxBulletCount; } }
    
    Rigidbody2D rbody;

    Animator anim;

    public int a = 0;

    // Start is called before the first frame update

    void Start()
    {
        currentHealth = 2;
        currentBulletCount = 2;
        invincibleTimer = 0;
        rbody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        UImanager.instance.UpdateHealthBar(currentHealth, maxHealth);
        UImanager.instance.UpdateBulletCount(currentBulletCount, maxBulletCount);
    }

    // Update is called once per frame
    void Update()
    {
        float moveX = Input.GetAxisRaw("Horizontal");
        float moveY = Input.GetAxisRaw("Vertical");

        Vector2 moveVector = new Vector2(moveX, moveY);
        if(moveVector.x!=0 || moveVector.y!=0)
        {
            lookDirection=moveVector;
        }
        anim.SetFloat("movex", moveVector.x);
        anim.SetFloat("movey", moveVector.y);
        Vector2 position = rbody.position;
        position.x += moveX *speed* Time.deltaTime;
        position.y += moveY * speed * Time.deltaTime;
        rbody.MovePosition(position);

        if (isInvincible)
        {
            invincibleTimer -= Time.deltaTime;
            if (invincibleTimer < 0)
            {
                isInvincible = false;
            }
        }
        if (Input.GetKeyDown(KeyCode.F)&&currentBulletCount>0)
        {
            ChangeBulletCount(-1);
            AudoManager.instance.AudioPlay(launchClip);
            GameObject bullet=Instantiate(bulletPrefab,rbody.position,Quaternion.identity);
            BulletController bc = bullet.GetComponent<BulletController>();
            if (bc != null)
            {
                bc.Move(lookDirection, 300);
            }
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            RaycastHit2D hit = Physics2D.Raycast(rbody.position, lookDirection, 1f,LayerMask.GetMask("NPC"));
            if(hit.collider!=null)
            {
                NPCmanager npc=hit.collider.GetComponent<NPCmanager>();
                if (npc != null)
                {
                    npc.ShowDialog();
                }
            }
        }
        if(currentHealth<=0)
        {
            Destroy(gameObject);
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        }
    }

    ///<param name="amount"></param>
    public void ChangeHealth(int amount)
    {
        if (amount < 0)
        {
            if (isInvincible == true)
            {
                return;
            }
            isInvincible = true;

            anim.SetTrigger("fix");

            AudoManager.instance.AudioPlay(hitClip);

            invincibleTimer = invincibleTime;
        }
        Debug.Log(currentHealth + "/" + maxHealth);

        currentHealth = Mathf.Clamp(currentHealth+amount,0,maxHealth);
        UImanager.instance.UpdateHealthBar(currentHealth,maxHealth);

        Debug.Log(currentHealth + "/" + maxHealth);
    }
    public void ChangeBulletCount(int amount)
    {
        currentBulletCount= Mathf.Clamp(currentBulletCount+amount,0,maxBulletCount);
        UImanager.instance.UpdateBulletCount(currentBulletCount,maxBulletCount);
    }
}
